﻿using System;
using System.Collections.Generic;
using Pumpkin.Utils;

namespace Pumpkin.FrameBase
{
    public class FrameModuleMgr : Singleton<FrameModuleMgr>
    {
        private Queue<IFrameModule> initSeq_ = new Queue<IFrameModule>();
        private Dictionary<string, IFrameModule> modules = new Dictionary<string, IFrameModule>();
        private Action onAllModulesLoaded;
        public void BeginInit()
        {
            InitNext();
        }

        public void InitNext()
        {
            if(initSeq_.Count == 0)
            {
                if(onAllModulesLoaded != null)
                {
                    onAllModulesLoaded.Invoke();
                }
            }
            else
            {
                initSeq_.Dequeue().Init();
            }
        }

        public void AddModule(IFrameModule module)
        {
            modules.Add(module.GetName(), module);
            initSeq_.Enqueue(module);
        }

        public void RemoveModule(string name)
        {
            modules.Remove(name);
        }

        public void RemoveModule(IFrameModule module)
        {
            modules.Remove(module.GetName());
        }

        public void RegistAllModulesLoaded(Action onAllLoaded)
        {
            if(onAllModulesLoaded == null)
            {
                onAllModulesLoaded = onAllLoaded;
            }
            else
            {
                onAllModulesLoaded += onAllLoaded;
            }
        }
    }
}
